package ldh.im.fxbase.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;

import java.net.URL;
import java.util.ResourceBundle;

public class ImagePageController implements Initializable {

    @FXML private ImageView imageView;
//    @FXML private Label tip;
    @FXML private Pane root;

    private Rectangle point2D = new Rectangle(0, 0);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        imageView.setOnMousePressed(e->{
            point2D.setX(e.getScreenX());
            point2D.setY(e.getScreenY());
            point2D.setWidth(imageView.getTranslateX());
            point2D.setHeight(imageView.getTranslateY());
        });
        imageView.setOnMouseDragged(e->{
            imageView.setTranslateX(point2D.getWidth() + e.getScreenX() - point2D.getX());
            imageView.setTranslateY(point2D.getHeight() + e.getScreenY() - point2D.getY());
        });

        imageView.setOnScroll(e->{
            scroll(e);
        });
        root.setOnScroll(e->scroll(e));
    }

    // 放大
    public void enlarge(MouseEvent actionEvent) {
        double scale = imageView.getScaleX() + 0.2;
        imageView.setScaleX(scale);
        imageView.setScaleY(scale);
    }

    // 缩小
    public void small(MouseEvent actionEvent) {
        double scale = imageView.getScaleX() - 0.2;
        imageView.setScaleX(scale);
        imageView.setScaleY(scale);
    }

    // 左旋转
    public void rotateLeft(MouseEvent actionEvent) {
        double rotate = imageView.getRotate();
        imageView.setRotate(rotate - 10);
    }

    // 右旋转
    public void rotateRight(MouseEvent actionEvent) {
        double rotate = imageView.getRotate();
        imageView.setRotate(rotate + 10);
    }

    // 下载
    public void download(MouseEvent actionEvent) {

    }

    public void setImage(Image image) {
        imageView.setImage(image);
//        if (imageView.getImage().getWidth() > 500) {
////            double scaleX = 500/imageView.getImage().getWidth();
////            imageView.setScaleX(scaleX);
//            imageView.setFitWidth(500);
//            imageView.setTranslateX(150);
//        }
//        if (imageView.getImage().getHeight() > 350) {
//            imageView.setFitHeight(350);
////            double scaleY = 350/imageView.getImage().getHeight();
////            imageView.setScaleY(scaleY);
//            imageView.setTranslateY((600 - 350)/2);
//        }
    }

    public void reset(MouseEvent mouseEvent) {
        imageView.setScaleY(1.0);
        imageView.setScaleX(1.0);
        imageView.setRotate(0);

        imageView.setTranslateX(0);
        imageView.setTranslateY(0);
    }

    private void scroll(ScrollEvent e) {
        double scale = imageView.getScaleX();
        if (e.getDeltaY() > 0) {
            scale += 0.1;
        } else {
            scale -= 0.1;
        }
        imageView.setScaleX(scale);
        imageView.setScaleY(scale);
    }

    private void intFit() {
        imageView.setFitHeight(0.0);
        imageView.setFitWidth(0.0);
    }
}
